{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Sum of Digits in Base K"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数学"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: sumBase"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #K 进制表示下的各位数字总和"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个整数 <code>n</code>（<code>10</code> 进制）和一个基数 <code>k</code> ，请你将 <code>n</code> 从 <code>10</code> 进制表示转换为 <code>k</code> 进制表示，计算并返回转换后各位数字的 <strong>总和</strong> 。</p>\n",
    "\n",
    "<p>转换后，各位数字应当视作是 <code>10</code> 进制数字，且它们的总和也应当按 <code>10</code> 进制表示返回。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>n = 34, k = 6\n",
    "<strong>输出：</strong>9\n",
    "<strong>解释：</strong>34 (10 进制) 在 6 进制下表示为 54 。5 + 4 = 9 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>n = 10, k = 10\n",
    "<strong>输出：</strong>1\n",
    "<strong>解释：</strong>n 本身就是 10 进制。 1 + 0 = 1 。\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 <= n <= 100</code></li>\n",
    "\t<li><code>2 <= k <= 10</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [sum-of-digits-in-base-k](https://leetcode.cn/problems/sum-of-digits-in-base-k/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [sum-of-digits-in-base-k](https://leetcode.cn/problems/sum-of-digits-in-base-k/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['34\\n6', '10\\n10']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        a = 0\n",
    "        while n:\n",
    "            aa = n %k\n",
    "            n = n//k\n",
    "            a += aa\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        res = 0\n",
    "        while n:\n",
    "            res += n%k\n",
    "            n //= k\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int: \n",
    "        a=0 \n",
    "        while n>0:\n",
    "            a=a+n%k\n",
    "            n=n//k\n",
    "        return a\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        sum =0\n",
    "        while n:\n",
    "            sum+= n%k\n",
    "            n//=k\n",
    "        return sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:       \n",
    "        ans=0\n",
    "        while n > 0:\n",
    "            ans+= n % k\n",
    "            n = n // k\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        d = []\n",
    "        while(1):\n",
    "            b = n%k\n",
    "            d.append(b)\n",
    "            n = n//k\n",
    "            if n <k:\n",
    "                d.append(n)\n",
    "                break\n",
    "        return sum(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        res = 0\n",
    "        while n:\n",
    "            res += n%k\n",
    "            n //= k\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        res=0\n",
    "        while n>=k:\n",
    "            n,remain=divmod(n,k)\n",
    "            res+=remain\n",
    "        return res+n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        nums=0\n",
    "        while n>=k:\n",
    "            nums+=n%k\n",
    "            n//=k\n",
    "        else:\n",
    "            nums+=n\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        ans=0\n",
    "        while n!=0:\n",
    "            ans+=n%k\n",
    "            n=(n-n%k)/k\n",
    "        return int(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        stack = ''\n",
    "        while n >= k:\n",
    "            stack += str(n%k)\n",
    "            #stack.append(n % k)\n",
    "            n //= k\n",
    "            #print('n=', n)\n",
    "            #print('stack=', stack)\n",
    "\n",
    "        stack += str(n)\n",
    "        #print('last stack = ', stack)\n",
    "        res = sum(list(map(int, stack)))\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        l = []\n",
    "        while n:\n",
    "            l.append(n % k)\n",
    "            n = n // k\n",
    "        return sum(l)        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        res = 0\n",
    "        while n:\n",
    "           res += n % k\n",
    "           n //= k\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        res = 0\n",
    "        while n >= k:\n",
    "            res += n % k\n",
    "            n = n // k\n",
    "        return res + n\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        ans = 0\n",
    "        while n > 0:\n",
    "            ans += n % k\n",
    "            n //=k\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        ans = 0\n",
    "        while n != 0:\n",
    "            ans += (n % k)\n",
    "            n //= k\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        res = 0\n",
    "        while n:\n",
    "            res += n%k\n",
    "            n //= k\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        # 10 -> k and calc sum\n",
    "        sum = 0\n",
    "        # loop\n",
    "        while n != 0 :\n",
    "            sum += int(n % k)\n",
    "            n /= k\n",
    "        return sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        nums = []\n",
    "        while n:\n",
    "            tmp = n % k\n",
    "            nums.append(tmp)\n",
    "            n //= k\n",
    "        return sum(nums)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        ret = 0\n",
    "        while n > 0:\n",
    "            ret += n % k\n",
    "            n = n // k\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        ans = 0\n",
    "        while n > 0:\n",
    "            ans += n % k\n",
    "            n //= k\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        \n",
    "        result = []\n",
    "\n",
    "        while n // k != 0:\n",
    "            result.append(n % k)\n",
    "            n = n // k\n",
    "        result.append(n % k)\n",
    "        \n",
    "        return sum(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        ans = 0\n",
    "        while n > 0:\n",
    "            ans += n % k\n",
    "            n //= k\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        res=0\n",
    "        while n:\n",
    "            res+=n%k\n",
    "            n//=k\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        sum = 0\n",
    "        while n > 0:\n",
    "            sum += n % k\n",
    "            n = n // k\n",
    "        return sum\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        lst = []\n",
    "        while n != 0:\n",
    "            lst.append(n % k)\n",
    "            n = n // k\n",
    "        return sum(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        if n < k:\n",
    "            return n\n",
    "\n",
    "        sum = 0\n",
    "        while n > 0:\n",
    "            sum += n % k\n",
    "            n = n // k\n",
    "\n",
    "        return sum\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        \n",
    "        ans=0\n",
    "        remainder=[]\n",
    "        while n > 0:\n",
    "            x = n % k\n",
    "            ans+=x\n",
    "            n = n // k\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        res = 0\n",
    "        while n > 0:\n",
    "            res += n % k\n",
    "            n //= k\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        result = n % k\n",
    "        while (n := n // k) != 0:\n",
    "            result += n % k\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        re = []\n",
    "        while n:\n",
    "            re.append(n%k)\n",
    "            n = n//k\n",
    "        return sum(re)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        res = 0\n",
    "        while n:\n",
    "            res += n%k\n",
    "            n //= k\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        count=0\n",
    "        while n:\n",
    "            count+=n%k\n",
    "            n//=k\n",
    "        return count\n",
    "        \n",
    "\n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        res=0\n",
    "        while n:\n",
    "            res+=n%k\n",
    "            n//=k\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        ret = 0\n",
    "        while n:\n",
    "            ret += n % k\n",
    "            n //= k\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        ans = 0\n",
    "        while n:\n",
    "            ans += n%k\n",
    "            n //= k\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        sum = 0\n",
    "        while n > 0:\n",
    "            sum = sum + (n % k)\n",
    "            n = n // k\n",
    "\n",
    "        return sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        yushu=0\n",
    "        d=n\n",
    "        sum=0\n",
    "        while d>0:\n",
    "            yushu=d%k\n",
    "            d=d//k\n",
    "            sum=sum+yushu\n",
    "        return sum\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        x=n%k\n",
    "        y=n//k\n",
    "        s=str(x)\n",
    "        while y!=0:\n",
    "            x=y%k\n",
    "            y=y//k\n",
    "            s=str(x)+s\n",
    "        return sum(int(i) for i in s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        fh = 0\n",
    "        while n:\n",
    "            fh += n%k\n",
    "            n //= k\n",
    "        return fh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        ans = 0\n",
    "        while n >= k:\n",
    "            ans += n % k\n",
    "            n //= k\n",
    "        ans += n\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        res=0\n",
    "        s=''\n",
    "        while n!=0:\n",
    "            s=str(n%k)+s\n",
    "            n//=k\n",
    "        for i in s:\n",
    "            res+=int(i)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        a = 0\n",
    "        while n:\n",
    "            a += n%k\n",
    "            n //= k\n",
    "        return a\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "def conversion(num, k):\n",
    "    res = ''\n",
    "    while num > 0:\n",
    "        num, re = divmod(num, k)\n",
    "        res += str(re)\n",
    "    return res[::-1]\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        s = conversion(n, k)\n",
    "\n",
    "        return sum(int(ch) for ch in s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        s=n  # 商\n",
    "        res=[] # 余数\n",
    "        while s>=k:\n",
    "            r=s%k\n",
    "            res.append(r)\n",
    "            s=int(s/k)\n",
    "        if s<k:\n",
    "            res.append(s)\n",
    "        res=res[::-1]\n",
    "        sum1=0\n",
    "        for i in res:\n",
    "            sum1=sum1+i\n",
    "        return sum1\n",
    "        # print(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        count=0\n",
    "        def coutall(n):\n",
    "            s_count=0\n",
    "            while n:\n",
    "                s_count+=n%10\n",
    "                n=n//10\n",
    "            return s_count\n",
    "        if n<k:\n",
    "            return coutall(n)\n",
    "        else:\n",
    "            while n>=k:\n",
    "                count+=n%k\n",
    "                n=n//k\n",
    "            return count+n\n",
    "\n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        s=n  # 商\n",
    "        res=[] # 余数\n",
    "        while s>=k:\n",
    "            r=s%k\n",
    "            res.append(r)\n",
    "            s=int(s/k)\n",
    "        if s<k:\n",
    "            res.append(s)\n",
    "        # res=res[::-1]\n",
    "        sum1=0\n",
    "        for i in res:\n",
    "            sum1=sum1+i\n",
    "        return sum1\n",
    "        # print(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        x=n%k\n",
    "        y=n//k\n",
    "        while y!=0:\n",
    "            x+=y%k\n",
    "            y=y//k\n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        res = 0\n",
    "        while n > 0:\n",
    "            res += n % k\n",
    "            n = n // k\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        def Chage(n,k):\n",
    "            s = \"\"\n",
    "            while n:\n",
    "                s = s + str(n%k)\n",
    "                n = n//k\n",
    "            return s\n",
    "        return sum([int(i) for i in Chage(n,k)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        l = []\n",
    "        while n >= k:\n",
    "            yushu=n%k\n",
    "            l.append(yushu)\n",
    "            n=n//k\n",
    "        l.append(n)\n",
    "        sum = 0\n",
    "        for i in l:\n",
    "            sum += i\n",
    "        return sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        sum = 0\n",
    "        while n > 0:\n",
    "            sum += n % k\n",
    "            n = n // k\n",
    "        return sum\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        temp = n\n",
    "        res = []\n",
    "\n",
    "\n",
    "        while temp >= k:\n",
    "            digit = temp % k\n",
    "            res.append(digit)\n",
    "            temp = temp // k\n",
    "        res.append(temp)\n",
    "\n",
    "        return sum(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        res = 0\n",
    "        while n:\n",
    "            res += n % k\n",
    "            n //= k\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        # a = []\n",
    "        # while n:\n",
    "        #     y = n % k\n",
    "        #     a.append(y)\n",
    "        #     n = n // k\n",
    "        # return sum(a)\n",
    "\n",
    "        sum = 0\n",
    "        while n:\n",
    "            sum += n % k\n",
    "            n = n // k\n",
    "        return sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        a=0\n",
    "        b=1\n",
    "        while b<=n:b*=k\n",
    "        b/=k\n",
    "        while n!=0:\n",
    "            a+=int(n/b)\n",
    "            n%=b\n",
    "            b/=k\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        kk=''\n",
    "        while n>0:\n",
    "            kk=str(n%k)+kk\n",
    "            n=n//k\n",
    "        #print(kk)\n",
    "        result=0\n",
    "        for letter in kk:\n",
    "            result=result+int(letter)\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        res = 0\n",
    "        while n != 0:\n",
    "            res += n % k\n",
    "            n = (n - n % k) // k\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def sumBase(self, n: int, k: int) -> int:\r\n",
    "        lastnum, Sum = n, 0\r\n",
    "        while lastnum:\r\n",
    "            lastnum, num = divmod(lastnum, k)\r\n",
    "\r\n",
    "            Sum += num\r\n",
    "\r\n",
    "        return Sum\r\n",
    "\r\n",
    "\r\n",
    "\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        res = 0\n",
    "        while n:\n",
    "            res += n % k\n",
    "            n //= k\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        res = 0\n",
    "        while n:\n",
    "            res += n%k\n",
    "            n //= k\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        s=0\n",
    "        while n:\n",
    "            s+=n%k\n",
    "            n//=k\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        ans = 0\n",
    "        while n>0:\n",
    "            ans += n%k\n",
    "            n = n//k\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        seat, res = 7, 0\n",
    "        while k ** (seat-1) > n or n >= k**seat:\n",
    "            seat -= 1\n",
    "            continue\n",
    "        while seat > 0:\n",
    "            d, n = divmod(n, k**(seat-1))\n",
    "            res += d\n",
    "            seat -= 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        if n < k:\n",
    "            return n\n",
    "        ans = []\n",
    "        while n:\n",
    "            n, m = divmod(n, k)\n",
    "            ans.append(m)\n",
    "        return sum(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        if n < k:\n",
    "            return n\n",
    "        ans = 0\n",
    "        while n:\n",
    "            n, m = divmod(n, k)\n",
    "            ans += m\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        def convertToBaseK(num, k):\n",
    "            result = ''\n",
    "            remain = num // k\n",
    "            result = str(num % k) + result\n",
    "            while remain > 0:\n",
    "                print(remain)\n",
    "                result = str(remain % k) + result\n",
    "                remain = remain // k\n",
    "            return result\n",
    "        \n",
    "        convert = convertToBaseK(n,k)\n",
    "        result = 0\n",
    "        for bit in convert:\n",
    "            result += int(bit)\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumBase(self, n: int, k: int) -> int:\n",
    "        ans = 0\n",
    "        while n > 0:\n",
    "            ans += n%k\n",
    "            n = n // k\n",
    "        return ans\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
